﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using QLHD.BUS;
using QLHD.Entity;

namespace QLHD.Ajax
{
    /// <summary>
    /// Summary description for HopDong
    /// </summary>
    public class HopDong : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string command = "";

            #region QueryString
            if (context.Request.Form["command"] != null)
                command = context.Request.Form["command"];
            else if (context.Request.QueryString["command"] != null)
                command = context.Request.QueryString["command"];

            string DanhMucHopDongID = "";
            if (context.Request.Form["DanhMucHopDongID"] != null)
                DanhMucHopDongID = context.Request.Form["DanhMucHopDongID"];
            else if (context.Request.QueryString["DanhMucHopDongID"] != null)
                DanhMucHopDongID = context.Request.QueryString["DanhMucHopDongID"];

            string HopDongID = "";
            if (context.Request.Form["HopDongID"] != null)
                HopDongID = context.Request.Form["HopDongID"];
            else if (context.Request.QueryString["HopDongID"] != null)
                HopDongID = context.Request.QueryString["HopDongID"];

            string khachhangID = "";
            if (context.Request.Form["KhachHangID"] != null)
                khachhangID = context.Request.Form["KhachHangID"];
            else if (context.Request.QueryString["KhachHangID"] != null)
                khachhangID = context.Request.QueryString["KhachHangID"];

            string saleID = "";
            if (context.Request.Form["SaleID"] != null)
                saleID = context.Request.Form["SaleID"];
            else if (context.Request.QueryString["SaleID"] != null)
                saleID = context.Request.QueryString["SaleID"];

            string obj = "";
            if (context.Request.Form["obj"] != null)
                obj = context.Request.Form["obj"];
            else if (context.Request.QueryString["obj"] != null)
                obj = context.Request.QueryString["obj"];

            string status = "";
            if (context.Request.Form["status"] != null)
                status = context.Request.Form["status"];
            else if (context.Request.QueryString["status"] != null)
                status = context.Request.QueryString["status"];

            string keyword = "";
            if (context.Request.Form["keyword"] != null)
                keyword = context.Request.Form["keyword"];
            else if (context.Request.QueryString["keyword"] != null)
                keyword = context.Request.QueryString["keyword"];

            string pageIndex = "";
            if (context.Request.Form["pageIndex"] != null)
                pageIndex = context.Request.Form["pageIndex"];
            else if (context.Request.QueryString["pageIndex"] != null)
                pageIndex = context.Request.QueryString["pageIndex"];

            string pageSize = "";
            if (context.Request.Form["pageSize"] != null)
                pageSize = context.Request.Form["pageSize"];
            else if (context.Request.QueryString["pageSize"] != null)
                pageSize = context.Request.QueryString["pageSize"];
            #endregion
            

            //insert dữ liệu chưa có thông tin thanh toán
            //obj = File.ReadAllText(@"E:\Hung\Projects\A\ADM-Tools\dataJson.json");
            //edit và lưu tiếp HopDongID 15 ,bổ sung thông tin thanh toán
            //obj = File.ReadAllText(@"E:\Hung\Projects\A\ADM-Tools\edit-all-data.json");

            var objHD = JsonConvert.DeserializeObject<QLHD.Entity.Manipulation.HopDong.objHD>(obj);

            JsonResponse responseData = new JsonResponse();

            if (!string.IsNullOrEmpty(command))
            {
                switch (command.ToLower())
                {
                    case "totalhopdong":
                        responseData = HopDong_CountHDByDanhMucHopDong(DanhMucHopDongID.ChangeType<long>());
                        break;
                    case "luuhopdong":
                        responseData = LuuHopdong(objHD, status.ChangeType<int>());
                        break;
                    case "updatesale":
                        responseData = UpdateSale(HopDongID.ChangeType<long>(), saleID.ChangeType<long>());
                        break;
                    case "updatekhachang":
                        responseData = UpdateKhachHang(HopDongID.ChangeType<long>(), khachhangID.ChangeType<long>());
                        break;
                    case "huyhopdong":
                        responseData = HuyHopdong(HopDongID.ChangeType<long>());
                        break;
                    case "edithopdong":
                        responseData = EditHopDong(HopDongID.ChangeType<long>());
                        break;
                    case "danhsachhopdong":
                        responseData = ListHopDong(keyword,pageIndex.ChangeType<Int32>(),pageSize.ChangeType<Int32>());
                        break;
                    case "editthongtinthanhtoan":
                        responseData = EditHopDong(HopDongID.ChangeType<long>());
                        break;
                }
            }
            else
            {
                responseData.Success = false;
                responseData.Message = "Không tìm thấy command.";
            }
            context.Response.Clear();
            context.Response.ContentType = "text/plain";
            context.Response.Write(JsonConvert.SerializeObject(responseData, Formatting.Indented));
        }

        private JsonResponse HopDong_CountHDByDanhMucHopDong(long HopDong_CountHDByDanhMucHopDong)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.HopDong_CountHDByDanhMucHopDong(HopDong_CountHDByDanhMucHopDong);
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse LuuHopdong(QLHD.Entity.Manipulation.HopDong.objHD objHD,int status)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.LuuHopdong(objHD, status);
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse HuyHopdong(long hopdongID)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.HuyHopdong(hopdongID);
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse UpdateSale(long hopdongID , long saleID)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.UpdateSale(hopdongID, saleID);
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse UpdateKhachHang(long hopdongID , long khachhangID)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.UpdateKhachHang(hopdongID, khachhangID);
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse EditHopDong(long HopDongID)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.EditHopdong(HopDongID);
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse ListHopDong(string keyword , int pageIndex , int pageSize)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var totalRecords = 0;
                var lstCategory = BoBussiness.ListHopdong(keyword, pageIndex, pageSize, out totalRecords);
                jsonResponse.PagerInfo = string.Format("{0} bài viết",totalRecords);
                jsonResponse.Pager = QLHD.BUS.Utils.PagerHelper.PageLinks(pageIndex, pageSize, totalRecords, 5, "javascript:QLHD.Table.initListHopDong({0})");
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse EdiThongTinThanhToan(long HopDongID)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.EditHopdong(HopDongID);
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

       

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
